import { setupWxShareOnRouteChange } from "./utils/app-actions";
import { setRequestQueue } from "./utils/wxlogin-queue";
import REQUEST, { httpConfig, setHosturl } from "./utils/request";
import { CITY_KEY, PROVINCE_KEY } from "./constants/sitevars";
// app.js
App({
  // towxml: require('/towxml/index'),
  $request(params) {
    return REQUEST.apply(this, [params]);
  },
  setHosturl() {
    setHosturl.apply(this);
  },
  globalData: {
    httpConfig,
    hosturl: "",
    userInfo: null,
    hasCheckFollowed: false, // 是否检查过关注公众号
    hasLogined: false,
    callbackWithCode: [], // 将请求放入队列
    options: {}, // 主要存放city='cd'这种参数，比较懒，每个页面跳转的时候都修改了这个值，
  },
  onLaunch(options) {
    this.globalData.invite_code = options.query.invite_code || "";
    this.globalData.options = options.query;
    this.mkCity(options);
    this.setHosturl();
    setupWxShareOnRouteChange();
    this.setRequestQueue(options);
  },
  onHide() {
    this.globalData.hasCheckFollowed = false;
  },
  /**
   * @description 初始化请求，获取用户信息等请求按顺序进行
   * @param {*} options
   */
  setRequestQueue(options) {
    return setRequestQueue(this, options);
  },
  mkCity(options) {
    const cacheNm = wx.getStorageSync(CITY_KEY) || '成都市';
    const citynm = options[CITY_KEY] || cacheNm;
    wx.setStorageSync(CITY_KEY, citynm);

    const cacheProvce = wx.getStorageSync(PROVINCE_KEY) || '四川省';
    const provincenm = options[PROVINCE_KEY] || cacheProvce;
    wx.setStorageSync(PROVINCE_KEY, provincenm);
  },
  /**
   * @description 如果url上面携带，优先从url上面获取，然后再在globaldata里面获取，最后从wxstorage里面获取，  如果进入了页面，用户手动改了城市，则以及手动设置的为准，并立马反馈到wxstorage里面，所以综合应该设置在storage里面，直接在里面获取
   * @param {*} option.cityId 
   */
  gtCity(options) {
    options = options || {};
    const cacheCitynm = wx.getStorageSync(CITY_KEY) || '成都市';
    let citynm = cacheCitynm;
    if (options[CITY_KEY]) {
      citynm = options[CITY_KEY];
    }
    return citynm
  },
  gtProvince(options) {
    options = options || {};
    const cacheProvncenm = wx.getStorageSync(PROVINCE_KEY) || '四川省';
    let prcnm = cacheProvncenm;
    if (options[PROVINCE_KEY]) {
      prcnm = options[PROVINCE_KEY];
    }
    return prcnm
  },

});
